package com.nobrain.uboard.DataManager;


import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.nobrain.uboard.Data.FBBaseFeed;
import com.nobrain.uboard.Data.FBComments;
import com.nobrain.uboard.Data.FBStatusFeed;

public class ParsingUtil {
	
	public static ArrayList<FBBaseFeed> FacebookData(String result){
		ArrayList<FBBaseFeed> feeds = new ArrayList<FBBaseFeed>();
		try{
			String id, name,message,type,crDate;
			JSONObject root = new JSONObject(result);
			if(root.has("data")){
				JSONArray datas = root.getJSONArray("data");
				int dataSize = datas.length();
				JSONObject data;
				FBBaseFeed feed = null;
				ArrayList<FBComments> comments;
				for(int i=0;i<dataSize;i++){
					
					comments = new ArrayList<FBComments>();
					data = datas.getJSONObject(i);
					
					type = data.getString("type");
					id = data.getString("id");
					message = data.getString("message");
					crDate = data.getString("created_time");
					JSONObject fromJ = data.getJSONObject("from");
					name = fromJ.getString("name");
					if(type.equals("status")){
						feed = new FBStatusFeed();
						feed.id = id;
						feed.type = type;
						feed.message = message;
						feed.crTime = crDate;
						feed.from = name;
					}
					if(data.has("comments")){
						JSONObject commentsJ = data.getJSONObject("comments");
						int count = commentsJ.getInt("count");
						if(count != 0){
							JSONArray commentsDataJ = commentsJ.getJSONArray("data");
							FBComments comment;
							for(int j=0;j<count;j++){
								comment =  new FBComments();
								JSONObject commentJ = commentsDataJ.getJSONObject(j);
								comment.message =   commentJ.getString("message");
								comment.name = commentJ.getJSONObject("from").getString("name");
								comments.add(comment);
							}
						}
					}
					feed.comments = comments;
					feeds.add(feed);
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		
		return feeds;
	}
}
